package com.climb.moive.filter;

import com.climb.moive.entity.Member;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author Administrator
 * 会员登录过滤
 */
@WebFilter(filterName ="MemberLoginFilter",urlPatterns = {"/memberCenter/*","/memberMovie/*"})
public class MemberLoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("进入会员登录过滤器");
        HttpServletRequest request=(HttpServletRequest)servletRequest;
        /*获取session中的member用来判断*/
        HttpSession session=request.getSession();
        Member member=(Member)session.getAttribute("member");
        HttpServletResponse response=(HttpServletResponse) servletResponse;
        if(member==null){
            /*放行*/
            request.getRequestDispatcher(request.getContextPath() + "/view/member/MemberLogin.jsp").forward(request,response);
            return;
        }
        /*正常放行*/
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
